package lc.二叉树.剑指Offer;

import utils.Node;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/26
 */
public class 剑指Offer36_二叉搜索树与双向链表 {

    Node pre, head;
    public Node treeToDoublyList(Node root) {

        if(root == null) return null;
        dfs(root);
        head.left = pre;
        pre.right = head;

        return head;
    }

    private void dfs(Node cur) {

        if (cur == null) return;

        dfs(cur.left);
        if (pre == null) {
            head  = cur;
        }
        else
            pre.right = cur;
        cur.left = cur;
        pre = cur;

        dfs(cur.right);
    }

}
